package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.exception.RoleException;
import com.uzhie.web.anno.Auth;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.module.Role;
import com.uzhie.web.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Zhangdequan on 2018/7/18.
 */
@RestController
@RequestMapping(value = "role")
public class RoleController extends CommController {

    @Autowired
    private RoleService roleService;

    /**
     * 根据条件查询角色列表
     *
     * @param body
     * @param page
     * @return
     */
    @Auth(tokenRequired = false)
    @RequestMapping(value = "list")
    public PayloadBean selectAllRoleByCondition(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {

        List<Role> list = null;
        Integer level = body.getInteger("level");
        try {
            list = roleService.selectAllRoleByCondition(body.getString("name"),
                    body.getString("createName"),
                    body.getString("minCreateTime"),
                    body.getString("maxCreateTime"),
                    body.getString("menuName"),
                    level,
                    page);
            return buildResponseWithPage(list, page);
        } catch (IllegalArgumentException e) {
            return buildResponseWithPage(null, page, e.getMessage());
        }
    }

    /**
     * 添加角色接口
     */
    @Auth
    @RequestMapping(value = "addRole")
    public PayloadBean addRole(@RequestBody(required = false) JSONObject body) {
        try {
            roleService.addRole(JSON.toJavaObject(body, Role.class));
            return buildResponse(null);
        } catch (RoleException e) {
            return buildResponse(null, e.getMessage());
        }
    }


    /**
     * 根据角色ID获取角色详情
     *
     * @return
     */
    @Auth
    @RequestMapping(value = "selectRoleById")
    public PayloadBean selectRoleById(@RequestBody(required = false) JSONObject body) {
        try {
            Role role = roleService.selectRoleById(body.getString("roleId"));
            return buildResponse(role);
        } catch (RoleException e) {
            return buildResponse(null, e.getMessage());
        }
    }

    /**
     * 更新角色信息
     *
     * @throws RoleException
     */
    @Auth
    @RequestMapping(value = "updateRole")
    public PayloadBean updateRole(@RequestBody(required = false) JSONObject body) {
        try {
            roleService.updateRole(JSON.toJavaObject(body, Role.class));
            return buildResponse(null);
        } catch (RoleException e) {
            return buildResponse(null, e.getMessage());
        }
    }

    /**
     * 根据角色id,批量修改角色信息数据库状态
     *
     * @throws RoleException
     */
    @Auth
    @RequestMapping(value = "updateStatus")
    public PayloadBean updateRoleStatusById(@RequestBody(required = false) JSONObject body) {
        try {
            int status = body.getInteger("status");
            List<Role> roleIds = JSONArray.parseArray(body.getString("roleIds"), Role.class);
            roleService.updateRoleStatusById(roleIds, status);
            return buildResponse(null);
        } catch (RoleException e) {
            return buildResponse(null, e.getMessage());
        }
    }


}
